//
//  ZCSearchVC.m
//  zctx
//
//  Created by ZhengDarren on 15/9/28.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCSearchVC.h"
#import "ZCSearchCell.h"
#import "ZCSeeResultVC.h"
#import "ZCRaiseVC.h"
#import "ZCSearchModel.h"
#import "DZWebViewController.h"

@interface ZCSearchVC ()<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate>

@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UIBarButtonItem *searchButton;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *bgImageView;
//data


@property (nonatomic, strong) NSArray *arrResult;

@end

@implementation ZCSearchVC


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
  
    self.title = @"搜一搜";
   
    self.arrResult = [NSArray array];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#ebeded"];
    self.navigationItem.titleView = self.searchBar;
    self.navigationItem.rightBarButtonItem = self.searchButton;
    NSLog(@"index = %@",theAppdelegate.tabbarIndex);
   
    [self initTableView];
    [self initBgView];
    //添加tableview
    
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    
    if ([theAppdelegate.tabbarIndex isEqualToString:@"1"]) {
        if (self.barcodeDictionary) {
            NSLog(@"dictt = %@",self.barcodeDictionary);
            self.searchBar.text = [NSString stringWithFormat:@"%@",[self.barcodeDictionary objectForKey:@"name"]];
            self.tableView.hidden = NO;
            self.bgImageView.hidden = YES;
        }
    }
}

-(void)initTableView{
    _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.hidden = YES;
    _tableView.tableFooterView = [[UIView alloc] init];
    _tableView.sectionFooterHeight = 0.0001;
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0,0,0,0));
    }];
}

-(void)initBgView{
    self.bgImageView = [[UIImageView alloc]init];
    [self.view addSubview:self.bgImageView];
    [self.bgImageView makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(@(SCREEN_WIDTH-80));
        make.height.mas_equalTo(@((SCREEN_WIDTH-80)/2.9));
        make.centerX.equalTo(self.view).with.offset(0);
        make.centerY.equalTo(self.view).with.offset(0);
    }];
    self.bgImageView.image = [UIImage imageNamed:@"search_shiping"];
    
}

#pragma mark - request 搜索请求

- (void)requestMainList {
    NSLog(@"text = %@",self.searchBar.text);
    
    NSString *searchText = [NSString stringWithFormat:@"%@",self.searchBar.text];
    if (searchText.length<=0) {
        return;
    }
    NSMutableString *ms = [[NSMutableString alloc] initWithString:searchText];
    if ([searchText length]) {
        if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformMandarinLatin, NO)) {
            NSLog(@"pinyin: %@", ms);
        }
        if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformStripDiacritics, NO)) {
            NSLog(@"pinyin: %@", ms);
        }
    }
    NSString *Pinyin = [ms stringByReplacingOccurrencesOfString:@" " withString:@","];
    ProgressHUDStr;
    [NetworkManager requestProductGetInfoByKeywordsWithMid:UserModel.mid keywords:searchText keywords_py:Pinyin success:^(NSDictionary *dictResponse) {
        ProgressDismiss;
        NSArray *array = (NSArray *)[dictResponse objectForKey:@"content"];
        self.arrResult = array;
        if (self.arrResult) {
            self.tableView.hidden = NO;
            self.bgImageView.hidden = YES;
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        ProgressDismiss;
    }];
}

#pragma mark - button click

- (void)searchButtonClick {
    [_searchBar resignFirstResponder];
    [self requestMainList];
}

#pragma mark - UISearchBar Delegate Methods

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [_searchBar resignFirstResponder];
    [self requestMainList];
}

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    if (searchBar.text) {
        self.tableView.hidden = NO;
        self.bgImageView.hidden = YES;
    }else{
        self.tableView.hidden = YES;
        self.bgImageView.hidden = NO;
    }
}

#pragma mark - UITableView DataSource & Delegate Methodsd

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (self.barcodeDictionary) {
        return 1;
    }
    return self.arrResult.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIndentity = @"Cell";
    ZCSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentity];
    if (!cell) {
        cell = [[ZCSearchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentity];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    ZCSearchModel *model;

    if (self.barcodeDictionary) {
        model = [ZCSearchModel modelObjectWithDictionary:self.barcodeDictionary];
    }
    if (self.arrResult.count>0) {
        model = [ZCSearchModel modelObjectWithDictionary:self.arrResult[indexPath.row]];
    }
    cell.titleLable.text = [NSString stringWithFormat:@"%@",model.name];
    if (model.isurl ==0) {
        if (model.itemCount ==0) {
            cell.infoLable.text = @"发起检测";
            cell.iconImgView.image = [UIImage imageNamed:@"search_icon_n"];
            cell.infoLable.textColor = [UIColor colorWithHexString:@"#f28a8a"];
        }else{
            theAppdelegate.tabbarIndex = @"2";
            cell.infoLable.text = [NSString stringWithFormat:@"%d条检测记录",model.itemCount];
            cell.iconImgView.image = [UIImage imageNamed:@"search_icon_p"];
            cell.infoLable.textColor = [UIColor colorWithHexString:@"#666666"];
        }
    }else{
        if (model.qualified == 0) {
            cell.infoLable.text = @"不合格";
            cell.infoLable.textColor = [UIColor colorWithHexString:@"#f28a8a"];
            cell.iconImgView.image = [UIImage imageNamed:@"search_icon_n"];
        }else{
            cell.infoLable.text = @"合格";
            cell.iconImgView.image = [UIImage imageNamed:@"search_icon_p"];
            cell.infoLable.textColor = MainColor;
        }
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZCSearchModel *model;
    if (self.barcodeDictionary) {
        NSLog(@"11111");
        model = [ZCSearchModel modelObjectWithDictionary:self.barcodeDictionary];
    }
    if (self.arrResult.count>0) {
        NSLog(@"2222");
        model = [ZCSearchModel modelObjectWithDictionary:self.arrResult[indexPath.row]];
    }
    if (model.isurl ==1) {
        DZWebViewController *vc = [[DZWebViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.strURL = model.url;
        vc.title = model.name;
        [self.navigationController pushViewController:vc animated:YES];
        
    }else{
        if (model.itemCount==0) {
            ZCRaiseVC *raise = [[ZCRaiseVC alloc]init];
            raise.hidesBottomBarWhenPushed = YES;
            raise.pushpjName = model.name;
            [self.navigationController pushViewController:raise animated:YES];
        }else{
            ZCSeeResultVC *seeresult = [[ZCSeeResultVC alloc]init];
            seeresult.pushArray = model.items;
            
            seeresult.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:seeresult animated:YES];
        }
    }
}

#pragma mark - instance

- (UISearchBar *)searchBar {
    if (!_searchBar) {
        _searchBar = [[UISearchBar alloc] init];
        _searchBar.placeholder = @"输入关键字搜索";
        _searchBar.tintColor = [UIColor grayColor];
        _searchBar.delegate = self;
    }
    return _searchBar;
}

- (UIBarButtonItem *)searchButton {
    if (!_searchButton) {
        _searchButton = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(searchButtonClick)];
    }
    return _searchButton;
}

//- (UITableView *)tableView {
//    if (!_tableView) {
//        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
//        _tableView.delegate = self;
//        _tableView.dataSource = self;
//        _tableView.hidden = YES;
//        _tableView.tableFooterView = [[UIView alloc] init];
//        _tableView.sectionFooterHeight = 0.0001;
//        [self.view addSubview:_tableView];
//        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0,0,0,0));
//        }];
//        
//    }
//    return _tableView;
//}

@end
